﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CapokunMvcTest.Models;
using CapokunMvcTest;

namespace CapokunMvcTest.Controllers
{ 
    public class SongController : Controller
    {
        private CapokunContext db = new CapokunContext();

		/// <summary>
		/// if id != null, it means, a specific song is asked, look for it and return it, else returns all songs
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public ActionResult Favorites()
		{
			///list of favorites is for the time being handled by html5storage, will be migrated to Facebook og
			return View();
		}

		/// <summary>
		/// show all songs
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public ActionResult All()
		{
			List<Song> returned = new List<Song>();
			IEnumerable<Song> songs = CapokunContext.GetAllSongs();

			returned = songs.OrderBy(x => x.SongId).ToList();
			return View(returned);
		}

		/// <summary>
		/// find a specific song
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public ActionResult Retrieve(string id)
		{
			Song returnedSong = null;
			IEnumerable<Song> songs = CapokunContext.GetAllSongs();

			foreach (Song song in songs)
			{
				if (song.SongId == id)
				{
					returnedSong = song;
					break;

				}
			}

			return View(returnedSong);
		}

		/// <summary>
		/// find a specific song
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public ActionResult OgTest(string id)
		{
			Song returnedSong = null;
			IEnumerable<Song> songs = CapokunContext.GetAllSongs();

			foreach (Song song in songs)
			{
				if (song.SongId == id)
				{
					returnedSong = song;
					break;

				}
			}

			return View(returnedSong);
		}

        //
        // GET: /Song/

        public ViewResult Index()
        {
            return View(db.Songs.ToList());
        }

        //
        // GET: /Song/Details/5

        public ViewResult Details(string id)
        {
            Song song = db.Songs.Find(id);
            return View(song);
        }

        //
        // GET: /Song/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Song/Create

        [HttpPost]
        public ActionResult Create(Song song)
        {
            if (ModelState.IsValid)
            {
                db.Songs.Add(song);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(song);
        }
        
        //
        // GET: /Song/Edit/5
 
        public ActionResult Edit(string id)
        {
            Song song = db.Songs.Find(id);
            return View(song);
        }

        //
        // POST: /Song/Edit/5

        [HttpPost]
        public ActionResult Edit(Song song)
        {
            if (ModelState.IsValid)
            {
                db.Entry(song).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(song);
        }

        //
        // GET: /Song/Delete/5
 
        public ActionResult Delete(string id)
        {
            Song song = db.Songs.Find(id);
            return View(song);
        }

        //
        // POST: /Song/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {            
            Song song = db.Songs.Find(id);
            db.Songs.Remove(song);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}